﻿namespace WebServiceExtensions.WCFClientLogger
{
	#region Namespaces

	using System;
	using System.Diagnostics;

	using WebServiceExtensions.Common;
	using WebServiceExtensions.Configurations.Common;

	#endregion

	/// <summary>
	///   The wcf soap trace listner.
	/// </summary>
	public class WcfSoapTraceListner : TraceListener
	{
		#region Fields (1) 

		/// <summary>
		///   The logger.
		/// </summary>
		private readonly IWcfLogger logger;

		#endregion Fields 

		#region Constructors (1) 

		/// <summary>
		///   Initializes a new instance of the <see cref = "WcfSoapTraceListner" /> class.
		///   Initializes a new instance of the <see cref = "T:System.MarshalByRefObject" /> class.
		/// </summary>
		public WcfSoapTraceListner()
		{
			if (WebServiceExtensionConfigurationView.Exists())
			{
				WebServiceExtensionConfigurationView configView = new WebServiceExtensionConfigurationView();
				string[] type = configView.WcfMessageLogger.Type.Split(
					new[] { Constants.CommaSeperator }, StringSplitOptions.RemoveEmptyEntries);

				this.logger = (IWcfLogger)AppDomain.CurrentDomain.CreateInstance(type[1], type[0]).Unwrap();
			}
		}

		#endregion Constructors 

		#region Methods (2) 

		// Public Methods (2) 

		/// <summary>
		///   When overridden in a derived class, writes the specified message to the listener you create in the derived class.
		/// </summary>
		/// <param name = "message">
		///   A message to write.
		/// </param>
		public override void Write(string message)
		{
			if (this.logger != null)
			{
				this.logger.LogMessage(message);
			}
		}

		/// <summary>
		///   When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
		/// </summary>
		/// <param name = "message">
		///   A message to write.
		/// </param>
		public override void WriteLine(string message)
		{
			if (this.logger != null)
			{
				this.logger.LogMessage(message + "\n");
			}
		}

		#endregion Methods 
	}
}